package cn.wjzhang.middle.modules.area.service.impl;

import cn.wjzhang.core.constant.DelConstant;
import cn.wjzhang.core.exception.BootException;
import cn.wjzhang.middle.modules.area.beans.vo.MidProvincesVo;
import cn.wjzhang.middle.modules.area.beans.entity.MidProvinces;
import cn.wjzhang.middle.modules.area.mapper.MidProvincesMapper;
import cn.wjzhang.middle.modules.area.beans.request.MidProvincesAddRequest;
import cn.wjzhang.middle.modules.area.beans.request.MidProvincesEditRequest;
import cn.wjzhang.middle.modules.area.beans.request.MidProvincesListRequest;
import cn.wjzhang.middle.modules.area.service.MidProvincesService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 省级（省份、直辖市、自治区）Service实现层
 *
 * @author wjzhang
 * @date 2023-06-28 18:48:28
 */
@Service
public class MidProvincesServiceImpl extends ServiceImpl<MidProvincesMapper, MidProvinces> implements MidProvincesService {

    @Resource
    private MidProvincesMapper midProvincesMapper;

    @Override
    public void removeByIdLogic(Long id) {
        MidProvinces entity = this.getById(id);
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        entity.setDelFlag(DelConstant.DEL);
        this.updateById(entity);
    }

    @Override
    public IPage<MidProvincesVo> listWithPage(MidProvincesListRequest param) {
        Page<MidProvincesVo> page = new Page<>(param.getPageNo(), param.getPageSize());
        List<MidProvincesVo> list = midProvincesMapper.listWithPage(page, param);
        return page.setRecords(list);
    }

    @Override
    public MidProvincesVo getDTO(Long id) {
        MidProvinces entity = this.getById(id);
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        MidProvincesVo vo = new MidProvincesVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public Long addMidProvinces(MidProvincesAddRequest addRequest) {
        MidProvinces entity = new MidProvinces();
        BeanUtils.copyProperties(addRequest, entity);
        this.save(entity);
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = {Throwable.class})
    public void editMidProvinces(MidProvincesEditRequest editRequest) {
        MidProvinces entity = this.getById(editRequest.getId());
        if (Objects.isNull(entity) || entity.getDelFlag() == 1) {
            throw new BootException("查询对象不存在或者已经被删除");
        }
        BeanUtils.copyProperties(editRequest, entity);
        this.updateById(entity);
    }
}
